<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="batchList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="cardNo" />
      <el-table-column label="评审对象" align="center" prop="objectName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >打分</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >提交</el-button>
        </template>
      </el-table-column>
      <el-table-column label="总分" align="center" prop="reviewType" />
      <el-table-column label="评分时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="批次名称" prop="batchName">
          <el-input v-model="form.batchName" placeholder="请输入批次名称" />
        </el-form-item>
        <el-form-item label="起始时间" prop="startTime">
          <el-date-picker clearable
                          v-model="form.startTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择起始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="

终止时间
" prop="endTime">
          <el-date-picker clearable
                          v-model="form.endTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择

终止时间
">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="评审状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入评审状态" />
        </el-form-item>
        <el-form-item label="评审时间" prop="reviewTime">
          <el-date-picker clearable
                          v-model="form.reviewTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择评审时间">
          </el-date-picker>
        </el-form-item>
        <file-upload :fileType="['pdf','doc','docx']"/>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBatch, getBatch, delBatch, addBatch, updateBatch } from "@/api/review/batch";
import axios from "axios";

export default {
  name: "Batch",
  dicts: ['review_type', 'sys_job_group'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      batchList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        batchName: null,
        reviewType: null,
        startTime: null,
        endTime: null,
        state: null,
        reviewTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      fileList: [],
      uploadUrl: 'http://127.0.0.1/dev-api/review/batch/upload',
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listBatch(this.queryParams).then(response => {
        this.batchList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        batchId: null,
        batchName: null,
        reviewType: null,
        startTime: null,
        endTime: null,
        state: null,
        reviewTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.batchId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const batchId = row.batchId || this.ids
      getBatch(batchId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改【请填写功能名称】";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.batchId != null) {
            updateBatch(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBatch(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const batchIds = row.batchId || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + batchIds + '"的数据项？').then(function() {
        return delBatch(batchIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/batch/export', {
        ...this.queryParams
      }, `batch_${new Date().getTime()}.xlsx`)
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      // window.open('/pdf/123.pdf', '_blank')
      console.log(file);
      // window.open(file.response)
      // if (file) {
      //   const addTypeArray = file.name.split(".");
      //   const addType = addTypeArray[addTypeArray.length - 1];
      //   console.log(addType);
      //   if (addType === "pdf") {
      //     let routeData = this.$router.resolve({
      //       path: "/insurancePdf",
      //       query: { url: file.response, showBack: false },
      //     });
      //     window.open(routeData.href, "_blank");
      //   }
      //   //".rar, .zip, .doc, .docx, .xls, .txt, .pdf, .jpg,  .png, .jpeg,"
      //   else if (addType === "doc" || addType === "docx" || addType === "xls") {
      //     window.open(
      //       "http://view.officeapps.live.com/op/view.aspx?src=" + file.response
      //     );
      //   } else if (addType === "txt") {
      //     window.open(file.response);
      //   } else if (["png", "jpg", "jpeg"].includes(addType)) {
      //     window.open(file.response);
      //   } else if (addType === "rar" || addType === "zip") {
      //     this.$message({
      //       message: "该文件类型暂不支持预览",
      //       type: "warning",
      //     });
      //     return false;
      //   }
      // }
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${ file.name }？`);
    },
    //测试上传文件(注意web的上下文)
    handleBeforeUpload(file) {
      console.log("开始上传，上传的文件为：" + file)
      let formData = new FormData();
      formData.append("multipartFiles", file);
      axios({
        method: 'post',
        url: 'http://127.0.0.1/dev-api/review/batch/upload',
        data: formData,
        headers: {'Content-Type': 'multipart/form-data'}
      }).then((res) => {
        console.log("文件上传返回：" + res)
      }).catch(error => {
        console.log("文件上传异常:" + error)
      })

      // this.uploadUrl ='http://192.168.43.152:8089/file/upload'
    }
  }
};
</script>
